package com.yanqu.road.dao.impl.wingroom.chakra;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.wingroom.UserWingRoomShareEvent;
import com.yanqu.road.entity.wingroom.chakra.WingRoomChakraPatronsAddItem;
import com.yanqu.road.entity.wingroom.chakra.data.UserWingRoomChakra;
import com.yanqu.road.entity.wingroom.chakra.data.UserWingRoomChakraBeVisitNum;
import com.yanqu.road.entity.wingroom.chakra.data.UserWingRoomChakraDetail;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class WingRoomChakraBeVisitNumDaoImpl extends TempDataObjectDao<UserWingRoomChakraBeVisitNum> {

    private static String insertSql = "INSERT INTO `t_u_wingroom_chakra_be_visit_num`(`userId`, `beVisitNum`, `lastRecoverTime`) VALUES (?, ?, ?)";
    @Override
    protected boolean addData(UserWingRoomChakraBeVisitNum data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBeVisitNum()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastRecoverTime()));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "UPDATE `t_u_wingroom_chakra_be_visit_num` SET `beVisitNum` = ?, `lastRecoverTime` = ? WHERE `userId` = ?";
    @Override
    protected boolean updateData(UserWingRoomChakraBeVisitNum data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getBeVisitNum()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastRecoverTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserWingRoomChakraBeVisitNum parseData(ResultSet rs) throws SQLException {
        return new UserWingRoomChakraBeVisitNum(rs.getLong("userId"), rs.getInt("beVisitNum"), rs.getLong("lastRecoverTime"));
    }

    /**
     * 获取数据
     * @return
     */
    public Map<Long, UserWingRoomChakraBeVisitNum> getUserWingRoomChakraBeVisitNumMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, UserWingRoomChakraBeVisitNum> dataMap = new ConcurrentHashMap<>();
        String selectSql = "select * from t_u_wingroom_chakra_be_visit_num";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(selectSql);
            rs = call.executeQuery();
            while (rs.next()) {
                UserWingRoomChakraBeVisitNum data = parse(rs);
                dataMap.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }
}
